﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Harkon.AppServer.cOne.Registry;
using Castle.Core.Interceptor;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Channels;
using log4net;
using Harkon.AppServer.cOne.Service;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using System.Threading;

namespace Harkon.AppServer.cOne.Service
{
    public class ServiceProxy : IInterceptor
    {
        private static ILog logger = LogManager.GetLogger(typeof(ServiceProxy));
        public IRegistry ServiceRegistry { get; set; }

        private object RealTarget { get; set; }
        private ServiceName WrappedServiceName { get; set; }
        private bool initialized = false;

        public ServiceProxy(ServiceName serviceName)
        {
            WrappedServiceName = serviceName;
        }

        #region IInterceptor Members

        public void Intercept(IInvocation invocation)
        {
            if (invocation.Method.Name.Contains("Init") || invocation.Method.Name.Contains("Start") || invocation.Method.Name.Contains("Stop"))
                return;

            if (!initialized)
            {
                RealTarget = ServiceRegistry.GetService(WrappedServiceName);
            }

            invocation.ReturnValue = invocation.Method.Invoke(RealTarget, invocation.Arguments);
        }

        #endregion
    }
}
